package cn.bigway.app.weavingdesigner.util;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class WdIntTextField extends JTextField {
	private static final long serialVersionUID = -3069281730732043717L;

	public WdIntTextField(int width, final int defVal, final int minVal,
			final int maxVal) {
		super(width);

		setText("" + defVal);

		setInputVerifier(new InputVerifier() {
			@Override
			public boolean verify(JComponent component) {
				JTextField textField = (JTextField) component;
				Integer val = getValue();
				if (null == val || val.intValue() < minVal
						|| val.intValue() > maxVal) {
					textField.selectAll();
					JOptionPane.showMessageDialog(component, "need " + minVal
							+ "~" + maxVal, "Error Dialog",
							JOptionPane.ERROR_MESSAGE);
					return false;
				}
				return true;
			}
		});
	}

	public Integer getValue() {
		try {
			return Integer.valueOf(getText());
		} catch (Exception e) {
			return null;
		}
	}
}
